package com.swu.pad.comment.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.swu.pad.activity.dao.ActivityDao;
import com.swu.pad.activity.entity.Activity;
import com.swu.pad.comment.entity.CommentQuestion;
import com.swu.pad.comment.entity.CommentType;
import com.swu.pad.comment.entity.FbComment;
import com.swu.pad.comment.entity.FbCommentQua;
import com.swu.pad.comment.service.CommentTemplateService;
import com.swu.pad.comment.service.FbCommentService;
import com.swu.pad.system.entity.User;
import com.swu.pad.system.service.UserService;

@Controller
public class FbCommentController {
	@Autowired
	private FbCommentService fbcommentser;

	@Autowired
	private ActivityDao activityDao;
	@Autowired
	private UserService userService;
	@Autowired
	private CommentTemplateService commentTemplateService;

	@ResponseBody
	@RequestMapping(value = "/SaveData1")
	public String savaComment(

			String commentId, // 评论在服务器上的id
			String radioValue, // 评论属于哪一类
			String replayStar, // 评论的分数
			String timestamp, // 时间戳
			String AuthorID, // 创建评论的人
			String Commenter, // 被评论而且要给出评论分数的人
			String text) {

		System.out.println("authorId=" + AuthorID);
		System.out.println("开始评论");
		FbComment fbcomment = new FbComment();
		fbcomment.setCommentId(commentId);
		// fbcomment.setScore(Integer.parseInt(replayStar));
		fbcomment.setAuthorId(AuthorID);
		fbcomment.setTimestamp(timestamp);
		fbcomment.setCommenterId(Commenter);
		fbcomment.setType(radioValue);
		// fbcomment.setText(text);
		fbcommentser.saveComment(fbcomment);
		return "success";
	}

	@ResponseBody
	@RequestMapping(value = "/anyComment")
	public FbCommentQua anyComment(String activityId, String userid) {
		User user = userService.findUserById(userid);
		FbCommentQua fbcommentQua = new FbCommentQua();
		List authorList = fbcommentser.FindCommentByAuthorId(userid, activityId);
		List commenerList = fbcommentser.FindCommentByCommenerId(userid, activityId);
		fbcommentQua.setName(user.getRealname());
		fbcommentQua.setCommentCount(commenerList.size());
		fbcommentQua.setBecommentCount(authorList.size());
		fbcommentQua.setCommentScore(1);
		return fbcommentQua;
	}

	@ResponseBody
	@RequestMapping(value = "/anyCommentType")
	public List<CommentType> anyCommentType(String activityId, String userid) {
		Activity activity = activityDao.findOne(activityId);
		List<CommentQuestion> commentQuestionList = commentTemplateService.getTemplateQuestions(activity.getCommentTemplateId());
		List<CommentType> list = new ArrayList<CommentType>();
		for (int i = 0; i < commentQuestionList.size(); i++) {
			List tempList = fbcommentser.FindCommentByTypeAndUserId(commentQuestionList.get(i).getTitle(), userid, activityId);
			CommentType commentType = new CommentType();
			commentType.setTypeName(commentQuestionList.get(i).getQuestion());
			commentType.setTypeValue(tempList.size());
			list.add(commentType);
		}
		return list;

	}

	@ResponseBody
	@RequestMapping(value = "/getRelationship")
	public List<Map<String, Object>> getRelationship(String activityId, HttpSession session) {
		User user = (User) session.getAttribute("user");
		return fbcommentser.getRelationship(activityId, user.getId());
	}
}
